package com.yet.source;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.core.env.MapPropertySource;

import com.yet.center.ConfigCenter;

public class CenterPropertySource extends MapPropertySource {

	private final ConfigCenter configCenter;

	private final static ConcurrentMap<String, Object> source = new ConcurrentHashMap<>();

	private static final String PROPERTYSOURCE_NAME = "configCenter";

	public CenterPropertySource(ConfigCenter configCenter) {
		super(PROPERTYSOURCE_NAME, source);
		this.configCenter = configCenter;
		reLoadConfig();
	}

	public void reLoadConfig() {
		synchronized (source) {
			source.clear();
			source.putAll(configCenter.load());
		}
	}
	
}
